package com.thinkgem.jeesite.modules.jedispubsub;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

/**
 * Created by chengyajun on 16/9/20.
 */


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;
import com.thinkgem.jeesite.common.config.Global;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class JedisUtil {
	private static JedisPool jedisPool;
    private static SimplePropertyPreFilter filter;
    private static Logger log=Logger.getLogger(JedisUtil.class);
    static {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(10000);
        config.setMaxIdle(100);//20
        config.setMaxWaitMillis(100000);
        config.setTestOnBorrow(true);
        config.setLifo(true);
        jedisPool = new JedisPool(config, Global.getConfig("JEDIS_IP"), Integer.valueOf(Global.getConfig("JEDIS_PORT")), 10000,Global.getConfig("JEDIS_PWD"));
    }

    public static SimplePropertyPreFilter getFilter() {
        if (filter == null) {
            filter = new SimplePropertyPreFilter(); // 构造方法里，也可以直接传需要序列化的属性名字
            filter.getExcludes().add("admin");
            filter.getExcludes().add("currentUser");
            filter.getExcludes().add("remarks");
            filter.getExcludes().add("updateBy");
            filter.getExcludes().add("currentUser");
            filter.getExcludes().add("page");
            filter.getExcludes().add("sqlMap");
            filter.getExcludes().add("roleIdList");
            filter.getExcludes().add("roleList");
            filter.getExcludes().add("roleNames");
            filter.getExcludes().add("searchFromPage");
            filter.getExcludes().add("skuAllotInfo");
            return filter;
        } else
            return filter;
    }


    /**
     * set Object
     */
    public static void setObject(String key,Object object,  int time) {
        Jedis jedis = null;
        try {
            if(object!=null) {
                jedis = jedisPool.getResource();
                jedis.set(key, JSON.toJSONString(object, getFilter()));
                jedis.expire(key, time);
            }
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }

    }

    /**
     * get Object
     */
    public static <T> T getObject(String key, Class<T> cls) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.get(key);
            if (value == null)
                return null;
            else
                return JSON.parseObject(value, cls);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
        return null;

    }

    public static <T> List<T> getList(String key, Class<T> cls) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.get(key);
            if (value == null)
                return null;
            else
                return  JSON.parseArray(value, cls);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
        return null;
    }

    public static List<Map<String, Object>> getListMap(String key) {


        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            String value = jedis.get(key);
            if (value == null)
                return null;
            else
                return JSON.parseObject(value,
                        new TypeReference<List<Map<String, Object>>>() {
                        });
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
        return null;
    }


    public  static Jedis getResource() {
        Jedis jedis = null;
        try {
            return jedisPool.getResource();
        } catch (Exception e) {

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
        return jedis;
    }


    /**
     * delete a key
     **/
    public static boolean delObject(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key.getBytes()) > 0;
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
        return false;
    }


    /**
     * 获取数据
     *
     * @param key
     * @return
     */
    public static String get(String key) {

        String value = null;
        Jedis jedis = null;
        try {
        	log.debug("get key:"+key);
            jedis = jedisPool.getResource();
            value = jedis.get(key);

        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }

        return value;
    }

    public static void close(Jedis jedis) {
        try {

            if (jedis != null) {
                jedis.close();

            }
        } catch (Exception e) {
            if (jedis.isConnected()) {
                jedis.close();
            }
        }

    }


    public static void set(String key, String value) {

        Jedis jedis = null;
        try {
        	log.debug("set key:"+key);
            if(value!=null&&value.length()>0) {
                jedis = jedisPool.getResource();
                jedis.set(key, value);
            }
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
    }
    
    public static void incrBy(String key, Long value) {

        Jedis jedis = null;
        try {
        	log.debug("set key:"+key);
            if(value!=null&&value>0) {
                jedis = jedisPool.getResource();
                jedis.incrBy(key, value);
            }
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
    }

    public static void set(String key, String value, int time) {

        Jedis jedis = null;
        try {
        	log.debug("set key:"+key);
            if(value!=null&&value.length()>0) {
                jedis = jedisPool.getResource();
                jedis.set(key, value);
                jedis.expire(key, time);
            }
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
    }


    public static void hset(String key, String field, String value) {
        Jedis jedis = null;
        try {
            if(value!=null&&value.length()>0) {
                jedis = jedisPool.getResource();
                jedis.hset(key, field, value);
            }
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
    }

    /*
     * 增加过期时间
     */
    public static void hset(String key, String field, String value, int seconds) {
        Jedis jedis = null;
        try {
        	log.debug("hset key:"+key+"--field:"+field);
            jedis = jedisPool.getResource();
            jedis.hset(key, field, value);
            jedis.expire(key, seconds);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
    }


    /**
     * 获取数据
     *
     * @param key
     * @return
     */
    public static String hget(String key, String field) {

        String value = null;
        Jedis jedis = null;
        try {
        	log.debug("hget key:"+key+"--field:"+field);
            jedis = jedisPool.getResource();
            value = jedis.hget(key, field);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }

        return value;
    }

    public static void hdel(String key, String field) {

        Jedis jedis = null;
        try {
        	log.debug("hdel key:"+key+"--field:"+field);
            jedis = jedisPool.getResource();
            jedis.hdel(key, field);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
    }

    /**
     * 存储REDIS队列 顺序存储
     */
    public static void lpush(String key, String value) {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            jedis.lpush(key, value);

        } catch (Exception e) {

            //释放redis对象

            e.printStackTrace();

        } finally {

            //返还到连接池
            close(jedis);

        }
    }

    /**
     * 存储REDIS队列 顺序存储
     */
    public static String lpop(String key) {

        Jedis jedis = null;
        String value = null;
        try {

            jedis = jedisPool.getResource();
            value = jedis.lpop(key);

        } catch (Exception e) {

            //释放redis对象

            e.printStackTrace();

        } finally {

            //返还到连接池
            close(jedis);

        }
        return value;
    }

    /**
     * 存储REDIS队列 反向存储
     */
    public static void rpush(String key, String value) {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            jedis.rpush(key, value);

        } catch (Exception e) {

            //释放redis对象

            e.printStackTrace();

        } finally {

            //返还到连接池
            close(jedis);

        }
    }

    /**
     * 将列表 source 中的最后一个元素(尾元素)弹出，并返回给客户端
     */
    public static void rpoplpush(String key, String destination) {

        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            jedis.rpoplpush(key, destination);

        } catch (Exception e) {

            //释放redis对象

            e.printStackTrace();

        } finally {

            //返还到连接池
            close(jedis);

        }
    }

    /**
     * 获取队列数据
     *
     * @return
     */
    public static List<String> lpopList(String key) {

        List<String> list = null;
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            list = jedis.lrange(key, 0, -1);

        } catch (Exception e) {

            //释放redis对象

            e.printStackTrace();

        } finally {

            //返还到连接池
            close(jedis);

        }
        return list;
    }

    /**
     * 获取队列数据
     *
     * @return
     */
    public static String rpop(String key) {

        String bytes = null;
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            bytes = jedis.rpop(key);

        } catch (Exception e) {

            //释放redis对象

            e.printStackTrace();

        } finally {

            //返还到连接池
            close(jedis);

        }
        return bytes;
    }

    public static void hmset(Object key, Map<String, String> hash) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.hmset(key.toString(), hash);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();

        } finally {
            //返还到连接池
            close(jedis);

        }
    }

    public static void hmset(Object key, Map<String, String> hash, int time) {
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            jedis.hmset(key.toString(), hash);
            jedis.expire(key.toString(), time);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();

        } finally {
            //返还到连接池
            close(jedis);

        }
    }

    public static List<String> hmget(Object key, String... fields) {
        List<String> result = null;
        Jedis jedis = null;
        try {

            jedis = jedisPool.getResource();
            result = jedis.hmget(key.toString(), fields);

        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();

        } finally {
            //返还到连接池
            close(jedis);

        }
        return result;
    }

    public static Set<String> hkeys(String key) {
        Set<String> result = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.hkeys(key);

        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();

        } finally {
            //返还到连接池
            close(jedis);

        }
        return result;
    }

    public static List<String> lrange(String key, int from, int to) {
        List<String> result = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.lrange(key, from, to);

        } catch (Exception e) {
            //释放redis对象
            e.printStackTrace();

        } finally {
            //返还到连接池
            close(jedis);

        }
        return result;
    }

    public static Map<String, String> hgetAll(String key) {
        Map<String, String> result = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.hgetAll(key);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();

        } finally {
            //返还到连接池
            close(jedis);
        }
        return result;
    }

    public static void del(String key) {

        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(key);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
    }
    
    public static void delLike(String key) {
    	
    	Jedis jedis = null;
    	try {
    		jedis = jedisPool.getResource();
    	    Set<String> set = jedis.keys(key+ "*");
    	    Iterator<String> it = set.iterator();
    	    while (it.hasNext()) {
    	    	String keyStr = it.next();
    	    	jedis.del(keyStr);
    	    }
    		jedis.del(key);
    	} catch (Exception e) {
    		//释放redis对象
    		
    		e.printStackTrace();
    	} finally {
    		//返还到连接池
    		close(jedis);
    	}
    }

    
    
    public static long llen(String key) {

        long len = 0;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            len = jedis.llen(key);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
        return len;
    }

    public static boolean keyExists(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.exists(key);
        } catch (Exception e) {
            //释放redis对象

            e.printStackTrace();
        } finally {
            //返还到连接池
            close(jedis);
        }
        return false;
    }
    


    public static void main(String[] args) {
        try {
            String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";

            String value = "aaa";  // 长度不够
            System.out.println(value.matches(regex));

            value = "1111aaaa1111aaaaa";  // 太长
            System.out.println(value.matches(regex));

            value = "111111111"; // 纯数字
            System.out.println(value.matches(regex));

            value = "aaaaaaaaa"; // 纯字母
            System.out.println(value.matches(regex));

            value = "####@@@@#"; // 特殊字符
            System.out.println(value.matches(regex));

            value = "1111aaaa";  // 数字字母组合
            System.out.println(value.matches(regex));

            value = "aaaa1111"; // 数字字母组合
            System.out.println(value.matches(regex));

            value = "aa1111aa";	// 数字字母组合
            System.out.println(value.matches(regex));

            value = "11aaaa11";	// 数字字母组合
            System.out.println(value.matches(regex));

            value = "aa11aa11"; // 数字字母组合
            System.out.println(value.matches(regex));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }







}
